class Player extends Ship
{
  int grav;// 0 means no gravity, -1 pushes away, 1 pulls
  boolean gravPull;
  PShape forwardThrust;
  PShape rightThrust;
  PShape leftThrust;
  PShape rightReverse;
  PShape leftReverse;  
  boolean accelerating;
  boolean decelerating;
  boolean turningRight;
  boolean turningLeft;
  void setAccelerating(boolean newVal){accelerating = newVal;}
  void setDecelerating(boolean newVal){decelerating = newVal;}
  void setTurningLeft(boolean newVal){turningLeft = newVal;}
  void setTurningRight(boolean newVal){turningRight = newVal;}
  PShape shipShape2;
  float ringSize;
  float ringSize2;
  float ringSize3;

  
  void enableGravity()
  {
    if(gravPull == true)
    {
      grav = 1;//pulls them towards player
    }
    else
    {
      grav = -1;//pushes away from player
    }
  }
  
  void turnRight()
  {
    if(alive)
        direction += turnSpeed;
        rotateShapes(turnSpeed);
  }
  void turnLeft()
  {
    if(alive)
        direction -= turnSpeed;
        rotateShapes(-turnSpeed);
  }
  
  void rotateShapes(float rot){
    shipShape.rotate(rot);
    shipShape2.rotate(rot);
    forwardThrust.rotate(rot);
    rightThrust.rotate(rot);
    leftThrust.rotate(rot);
    rightReverse.rotate(rot);
    leftReverse.rotate(rot);
  }
  
  void oppositeGravity()
  {
    grav = -grav;
    gravPull = !gravPull;
  }
  
  void disableGravity()
  {
      grav = 0;//pushes away from player
  }
  
  void checkEnergy()
  {
    if(grav != 0)
    {
      if(!useEnergy(2))
      {
        grav = 0;
      }
    }
  }


  void draw(Camera c)
  {
    if( ((position.x + rad) > c.position.x - (width/2)) && ((position.x - rad) < c.position.x + (width/2)) && ((position.y + rad) > c.position.y - (height/2)) && ((position.y - rad)< c.position.y + (height/2)))
    {
      fill(100,100,100);
      stroke(100,100,100);
      pushMatrix();
      // translate(position.x - width / 2 - c.position.x + width/2, position.y - height / 2 - c.position.y + height/2); //TODO: Clean this up (though I assume this drawing code will be removed in favor of the shape() code)
      float drawX = position.x + (cos(direction + HALF_PI)*32) - (cos(direction)*sqrt(1024));
      float drawY = position.y + (sin(direction + HALF_PI)*32) - (sin(direction)*sqrt(1024));
     if(gravPull)
      {
        if(grav != 0)
          {
            ellipseMode(CENTER);
            noFill();
            stroke(200,20,20);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize,ringSize, 0, TWO_PI);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize2,ringSize2, 0, TWO_PI);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize3,ringSize3, 0, TWO_PI);            
            ellipseMode(CORNER);
            ringSize--;
            ringSize2--;                       
            ringSize3--;             
            if(ringSize < 25)
            {
              ringSize = 100;
              ringSize2 = 50;
              ringSize3 = 75;
            }
            else if(ringSize2 < 25)
            {
              ringSize = 75;
              ringSize2 = 100;
              ringSize3 = 50;
            }      
            else if(ringSize3 < 25)
            {
              ringSize = 50;
              ringSize2 = 75;
              ringSize3 = 100;
            }       
          }
        shape(shipShape, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
      }
 
      if(!gravPull)
      {
        if(grav != 0)
          {
            ellipseMode(CENTER);
            noFill();
            stroke(20,20,200);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize,ringSize, 0, TWO_PI);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize2,ringSize2, 0, TWO_PI);
            arc((width/2) + position.x - c.position.x, (height/2) + position.y - c.position.y, ringSize3,ringSize3, 0, TWO_PI);
            ellipseMode(CORNER);
            ringSize++;
            ringSize2++;
            ringSize3++;            
            if(ringSize > 100)
            {
              ringSize = 50;
              ringSize2 = 75;
              ringSize3 = 25;
            }
            else if(ringSize2 > 100)
            {
              ringSize = 25;
              ringSize2 = 50;
              ringSize3 = 75;
            }      
            else if(ringSize3 > 100)
            {
              ringSize = 75;
              ringSize2 = 25;
              ringSize3 = 50;
            }    
          } 
        shape(shipShape2, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64);
      }
      if(accelerating)
      {
      shape(forwardThrust, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
      }
      if(turningLeft)
      {
        if(decelerating)
        {
          shape(leftReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
        }
        else if(accelerating)
        {
          shape(rightThrust, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
        }
        else
        {
          shape(leftReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
          shape(rightThrust, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64);           
        }
      }
      if(turningRight)
      {
        if(decelerating)
        {
          shape(rightReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
        }
        else if(accelerating)
        {
          shape(leftThrust, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
        }
        else
        {
          shape(rightReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
          shape(leftThrust, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64); 
        }
      }
      else if(decelerating)
      {
          shape(rightReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64);  
          shape(leftReverse, (width/2) + drawX - c.position.x,(height/2) + drawY - c.position.y,64,64);        
      }
      
 
    
      popMatrix();
    }
  }
  
  void update()
  {
    checkHealth();
    if(alive)
    {
      updatePosition();
      if(firing)
        queueFire();
      applyPlanetGravity();
      checkCollisions();
      checkEnergy();
      addEnergy(0.5);
    }
  }
  
  Player(int xin, int yin, int massin)
  {
    super(xin, yin, massin);
    grav = 0;
    gravPull = true;
    topSpeed = 6.5;
    shipShape = loadShape("gfx/playerShip.svg");
    shipShape2 = loadShape("gfx/playerShip2.svg");
    forwardThrust = loadShape("gfx/playerThrust.svg");
    rightThrust = loadShape("gfx/playerRightThrust.svg");
    leftThrust = loadShape("gfx/playerLeftThrust.svg");
    rightReverse = loadShape("gfx/playerRightReverse.svg");
    leftReverse = loadShape("gfx/playerLeftReverse.svg");     
    rotateShapes(direction - HALF_PI);
    ringSize = 25;
    ringSize2 = 50;
    ringSize3 = 75;
  }
}
